package com.rongshu.view.penpal.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.penpal.adapter.VefityMemberAdapter;
import com.rongshu.view.penpal.bean.PenpalJoinVerifyResponse;
import com.rongshu.view.penpal.present.MemberVerifyPresenter;
import com.rongshu.view.penpal.view.MemberVerifyAdapterListener;
import com.rongshu.view.penpal.view.MemberVerifyView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2018/11/6.
 * description：审核加入笔友会页面
 */
public class MemberVerifyActivity extends BaseActivity<MemberVerifyPresenter> implements MemberVerifyView {
    @BindView(R.id.head_middle)
    TextView headMiddle;
    @BindView(R.id.head_right)
    TextView headRight;
    @BindView(R.id.member_list)
    RecyclerView memberList;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshView;
    @BindView(R.id.no_content)
    RelativeLayout noContent;
    private List<PenpalJoinVerifyResponse.ResponseBean> list;
    private VefityMemberAdapter adapter;
    private int penpalwill_id;
    private int pageIndex = 1;
    private int pageSize = 15;
    private int agreePosition;
    private String status;

    @Override
    protected int initLayout() {
        return R.layout.activity_member_verify;
    }

    @Override
    protected void initView() {
        headMiddle.setText("审核");
        refreshView.setEnableAutoLoadmore(false);
        list = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        memberList.setLayoutManager(linearLayoutManager);
        adapter = new VefityMemberAdapter(this, list);
        memberList.setAdapter(adapter);
        adapter.setAdapter(new MemberVerifyAdapterListener() {
            @Override
            public void agree(int position) {
                status = "pass";
                agreePosition = position;
                presenter.agreeJoinPenpal(penpalwill_id, list.get(position).getApply_userid(), "pass");
            }

            @Override
            public void refuse(int position) {
                status = "reject";
                agreePosition = position;
                presenter.agreeJoinPenpal(penpalwill_id, list.get(position).getApply_userid(), "reject");
            }
        });
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            penpalwill_id = intent.getIntExtra(Constant.PENPAL_ID, 0);
        }
        setPresenter(new MemberVerifyPresenter(this), this);
        presenter.getMemberList(penpalwill_id, pageIndex, pageSize);

        refreshView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageIndex = 1;
                refreshlayout.resetNoMoreData();
                presenter.getMemberList(penpalwill_id, pageIndex, pageSize);
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        refreshView.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                presenter.getMemberList(penpalwill_id, pageIndex, pageSize);
                refreshlayout.finishLoadmore(2000/*,false*/);//传入false表示刷新失败
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick({R.id.head_left})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_left:
                finish();
                break;

        }
    }

    @Override
    public void callBackMemberList(PenpalJoinVerifyResponse response) {
        if (pageIndex == 1) {
            list.clear();
        }
        if (refreshView.isRefreshing()) {
            refreshView.finishRefresh();
        }
        if (refreshView.isLoading()) {
            refreshView.finishLoadmore();
        }
        if (response != null && response.getResponse() != null) {
            list.addAll(response.getResponse());
            adapter.notifyDataSetChanged();
            if (list.size() == 0) {
                refreshView.setEnableRefresh(false);
                noContent.setVisibility(View.VISIBLE);
            }
            if (response.getResponse().size() == 0) {
                refreshView.setLoadmoreFinished(true);
            }
        }
    }

    @Override
    public void isAgreeJoinPenpal() {
        list.get(agreePosition).setAuditState(status);
        adapter.notifyDataSetChanged();
    }

}
